﻿/***********************************************************************
 *            Project: CoreCms
 *        ProjectName: 核心内容管理系统                                
 *                Web: https://www.corecms.net                      
 *             Author: 大灰灰                                          
 *              Email: jianweie@163.com                                
 *         CreateTime: 2022/1/31 21:45:10
 *        Description: 暂无
 ***********************************************************************/

using System;
using System.Threading.Tasks;
using CoreCms.Net.Configuration;
using CoreCms.Net.IServices;
using CoreCms.Net.Loging;
using Essensoft.Paylink.Alipay;
using Essensoft.Paylink.Alipay.Notify;
using InitQ.Abstractions;
using InitQ.Attributes;
using Newtonsoft.Json;

namespace CoreCms.Net.RedisMQ
{
    /// <summary>
    /// 支付宝支付成功后推送到接口进行数据处理
    /// </summary>
    public class AliPayNoticeSubscribe : IRedisSubscribe
    {
        private readonly ICoreCmsBillPaymentsServices _billPaymentsServices;

        public AliPayNoticeSubscribe(ICoreCmsBillPaymentsServices billPaymentsServices)
        {
            _billPaymentsServices = billPaymentsServices;
        }

        /// <summary>
        /// 支付宝支付成功后推送到接口进行数据处理
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        [Subscribe(RedisMessageQueueKey.AliPayNotice)]
        private async Task AliPayNotice(string msg)
        {
            try
            {
               var notify = JsonConvert.DeserializeObject<AlipayTradeAppPayNotify>(msg);
                if (notify is { TradeStatus: AlipayTradeStatus.Success })
                {
                    if (notify.TradeStatus == AlipayTradeStatus.Success)
                    {
                        var money = decimal.Parse(notify.TotalAmount);
                        await _billPaymentsServices.ToUpdate(notify.OutTradeNo,
                            (int)GlobalEnumVars.BillPaymentsStatus.Payed,
                            GlobalEnumVars.PaymentsTypes.alipay.ToString(), money, notify.TradeStatus,notify.TradeNo);
                    }
                    else
                    {
                        var money = decimal.Parse(notify.TotalAmount);
                        var message = notify.TradeStatus;
                        await _billPaymentsServices.ToUpdate(notify.OutTradeNo,
                            (int)GlobalEnumVars.BillPaymentsStatus.Other,
                            GlobalEnumVars.PaymentsTypes.alipay.ToString(), money, message, notify.TradeNo);
                    }
                }
                NLogUtil.WriteAll(NLog.LogLevel.Info, LogType.RedisMessageQueue, "支付宝支付成功后推送到接口进行数据处理", msg);
            }
            catch (Exception ex)
            {
                NLogUtil.WriteAll(NLog.LogLevel.Error, LogType.RedisMessageQueue, "支付宝支付成功后推送到接口进行数据处理", msg, ex);
                throw;
            }
            await Task.CompletedTask;
        }

    }
}
